package com.comleader.operator.service;

import static com.comleader.operator.common.constant.GlobalVariables.*;
import static com.comleader.operator.common.enums.CommonStateEnum.DISABLE;
import static com.comleader.operator.common.enums.CommonStateEnum.ENABLE;
import static com.comleader.operator.pojo.entity.table.NoncompliantConfigTableDef.NONCOMPLIANT_CONFIG;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Service;

import com.comleader.operator.common.page.PageQuery;
import com.comleader.operator.mapper.NoncompliantConfigMapper;
import com.comleader.operator.pojo.entity.NoncompliantConfigEntity;
import com.comleader.operator.pojo.param.NoncompliantConfigParam;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 不合规项配置表 服务层实现。
 *
 * @author 连新景
 * @since 1.0
 */
@Service
public class NoncompliantConfigService extends ServiceImpl<NoncompliantConfigMapper, NoncompliantConfigEntity> {

    @PostConstruct
    public void init() {
        List<NoncompliantConfigEntity> list = list(QueryWrapper.create().orderBy(NONCOMPLIANT_CONFIG.CODE.asc()));
        setCodeNameMap(CollStreamUtil.toMap(list, NoncompliantConfigEntity::getCode, NoncompliantConfigEntity::getName));
        // 筛选出启用的
        Map<Integer, List<String>> typeCodeMap =
            CollStreamUtil.groupBy(list.stream().filter(data -> ENABLE.getState().equals(data.getStatus())).collect(Collectors.toList()),
                NoncompliantConfigEntity::getType, Collectors.mapping(NoncompliantConfigEntity::getCode, Collectors.toList()));
        setTypeCodeMap(typeCodeMap);
    }


    public Page<NoncompliantConfigEntity> selectPage(NoncompliantConfigParam param) {
        QueryWrapper queryWrapper = QueryWrapper.create().from(NONCOMPLIANT_CONFIG)
            .where(NONCOMPLIANT_CONFIG.NAME.like(param.getName()).or(NONCOMPLIANT_CONFIG.CODE.like(param.getName())).when(StrUtil.isNotBlank(param.getName())))
            .and(NONCOMPLIANT_CONFIG.TYPE.eq(param.getType())).and(NONCOMPLIANT_CONFIG.STATUS.eq(param.getStatus())).orderBy(NONCOMPLIANT_CONFIG.ID.desc());
        return page(PageQuery.build(param), queryWrapper);
    }

    public boolean saveNoncompliantConfig(NoncompliantConfigEntity entity) {
        boolean bn = save(entity);
        if (bn) {
            updateCache(entity);
        }
        return bn;
    }

    public boolean updateNoncompliantConfig(NoncompliantConfigEntity entity) {
        boolean bn = updateById(entity);
        if (bn) {
            updateCache(entity);
        }
        return bn;
    }

    /**
     * @Description 修改状态
     * @Auhtor 连新景
     * @Date 2024/07/24 14:00
     * @param id
     * @param status
     * @return boolean
     */
    public boolean updateStatus(Integer id, Integer status) {
        NoncompliantConfigEntity configEntity = new NoncompliantConfigEntity();
        configEntity.setId(id);
        configEntity.setStatus(status);
        configEntity.setUpdateTime(LocalDateTime.now());
        boolean bn = updateById(configEntity);
        if (bn) {
            configEntity = getById(id);
            updateCache(configEntity);
        }
        return bn;
    }

    public boolean checkConfigUnique(NoncompliantConfigEntity configEntity) {
        NoncompliantConfigEntity tempConfig = getOne(QueryWrapper.create().where(NONCOMPLIANT_CONFIG.CODE.eq(configEntity.getCode())));
        return tempConfig == null || tempConfig.getId().equals(configEntity.getId());
    }

    public void updateCache(NoncompliantConfigEntity entity) {
        getCodeNameMap().put(entity.getCode(), entity.getName());
        if (entity.getStatus() == null || ENABLE.getState() == entity.getStatus()) {
            getTypeCodeMap().get(entity.getType()).add(entity.getCode());
        } else if (DISABLE.getState() == entity.getStatus()) {
            getTypeCodeMap().get(entity.getType()).remove(entity.getCode());
        }
    }

}